package com.android.picotts.ui.activity.pick;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;

import com.android.picotts.R;
import com.android.picotts.ui.activity.common.TemplateActivity;
import com.android.picotts.ui.activity.pick.AppListAdapter.AppInfo;
import com.android.picotts.ui.widget.TitleBar;
import com.android.picotts.ui.widget.TitleBar.OnTitleClickListener;

public class PickAppActivity extends TemplateActivity {
    public static final String KEY_APP = "_app";
    ListView mListView = null;
    AppListAdapter madapter = null;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setNoTitle();
        setContentView(R.layout.activity_pickapp);
        initViews();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        loadData();
    }
    
    @Override
    protected void setupTitleBar(TitleBar titleBar) {
        titleBar.setTitle("选择应用");
        titleBar.setOnTitleClickListener(new OnTitleClickListener() {
            @Override
            public void onRightClick() {
                 List<AppInfo> list = madapter.getChecks();
                if(list != null) {
                    Intent data = new Intent();
                    ArrayList<String> result = new ArrayList<String>();
                    for(AppInfo app : list) {
                        result.add(app.packageName);
                    }
                    data.putStringArrayListExtra(KEY_APP, result);
                    setResult(RESULT_OK, data);
                    finish();
                }else {
                    showToast("请先选择应用！");
                }
            }
        }, false, true);
    }
    
    private void initViews() {
        mListView=(ListView)findViewById(R.id.listview);  
        
        madapter = new AppListAdapter(this);
        mListView.setAdapter(madapter);
    }
    
    private void loadData() {
        if(madapter.getCount() > 0) return;
        
        showLoding("", "请稍后...");
        AsyncTask<Void, Void, List<AppInfo>> task = new AsyncTask<Void, Void, List<AppInfo>>(){

            @Override
            protected List<AppInfo> doInBackground(Void... params) {
                ArrayList<AppInfo> appList = new ArrayList<AppInfo>();
                List<PackageInfo> packages = getPackageManager().getInstalledPackages(0);
                for (int i = 0; i < packages.size(); i++) {
                    PackageInfo packageInfo = packages.get(i);                      
                    if((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)==0)
                    {
                        //非系统应用
                        AppInfo tmpInfo = new AppInfo();
                        tmpInfo.appName = packageInfo.applicationInfo.loadLabel(getPackageManager()).toString();
                        tmpInfo.packageName = packageInfo.packageName;
                        tmpInfo.versionName = packageInfo.versionName;
                        tmpInfo.appIcon = packageInfo.applicationInfo.loadIcon(getPackageManager());
                        appList.add(tmpInfo);
                    } else
                    {
                        //系统应用                　　　　　　　
                    }
                } 
                return appList;
            }
            
            @Override
            protected void onPostExecute(List<AppInfo> result) {
                hideLoding();
                madapter.setList(result);
            }
        };
        task.execute();
    }
    
    public static void startActivityForResult(Activity activity, int reqCode) {
        Intent it  = new Intent(activity, PickAppActivity.class);
        activity.startActivityForResult(it, reqCode);
    }
}
